﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace WebUI
{
    public partial class Roles : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);

            if (!IsPostBack)
            {
                POSSystem.BLL.Roles roles = POSSystem.BLL.Roles.GetAllActiveRoles();
                lstRoles.RoleList = roles;
                RefreshRoleList();
            }
        }

        public string GetRoleEditPageLink()
        {
            return "EditPages/EditRole.aspx?key=" + lstRoles.SourceObjectSessionID;
        }

        protected void btnAddRole_OnClick(object sender, EventArgs e)
        {
            lstRoles.CurrentPageIndex = 0;
            RefreshRoleList();
        }

        public void RefreshRoleList()
        {
            lstRoles.Filter = "Deleted = 0";
            lstRoles.CurrentPageIndex = 0;
            if (SessionUser.IsColumnNull(View_Users.ColumnNames.SystemAdmin) || !SessionUser.SystemAdmin)
            {
                lstRoles.Columns[13].Visible = false;
            }
            lstRoles.Refresh(); 
        }

        protected void btnSaveRole_OnClick(object sender, EventArgs e)
        {
            try
            {
                lstRoles.Update();
                RefreshRoleList();

                ShowHintMessage(panelErr, "");
            }
            catch (Exception ex)
            {
                ShowErrorMessage(panelErr, ex.Message);
            }
        }

        protected void btnRemoveRole_OnClick(object sender, EventArgs e)
        {
            try
            {
                lstRoles.DeleteCheckedItems();
                RefreshRoleList();
            }
            catch (Exception ex)
            {
                ShowErrorMessage(panelErr, ex.Message);
            }
        }
    }
}